package com.changpei.core.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.changpei.common.cache.ContentCache;
import com.changpei.common.cache.UserCache;
import com.changpei.common.result.Result;
import com.changpei.core.pojo.User;
import com.changpei.core.service.UserService;
import com.changpei.core.utils.SearchUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * <p>Title : User</p>
 * <p>Description : user的控制层</p>
 *
 * @author weibiao
 * @date 2020年3月7日
 */

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserCache userCache;

    @Autowired
    private ContentCache contentCache;

    /**
     * <p>Title : add</p>
     * <p>Description : 新增User数据</p>
     *
     * @param user
     * @return
     */
    @PostMapping(value = "/add")
    public Result<Long> add(@RequestBody User user) {
        // 调用UserService实现添加User
        if (userService.save(user)) {
            return Result.ok(user.getId());
        }
        return Result.failed();
    }

    /**
     * 用户操作
     * @param param
     * @return
     */
    @PostMapping(value = "/ops")
    public Result<JSONObject> ops(@RequestBody Map<String, Object> param) {
        return Result.ok(userService.ops(param));
    }

    /**
     * 是否操作过
     * @param param
     * @return
     */
    @PostMapping(value = "/operated")
    public Result<Boolean> operated(@RequestBody Map<String, Object> param) {
        return Result.ok(userService.operated(param));
    }

    /**
     * <p>Title : delete</p>
     * <p>Description : User根据ID数据</p>
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result<String> delete(@PathVariable Long id) {
        // 调用UserService实现根据主键删除
        if (userService.removeById(id)) {
            return Result.ok();
        }
        return Result.failed();
    }

    /**
     * <p>Title : update</p>
     * <p>Description : User根据ID修改</p>
     *
     * @param user
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result<String> update(@RequestBody User user, @PathVariable Long id) {
        // 设置主键值
        user.setId(id);
        // 调用UserService实现修改User
        if (userService.updateById(user)) {
            return Result.ok();
        }
        return Result.failed();
    }

    /**
     * <p>Title : findPage</p>
     * <p>Description : User分页搜索实现</p>
     *
     * @param pageNum  : 当前页
     * @param pageSize : 每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{pageNum}/{pageSize}")
    public Result<Page<User>> findPage(@PathVariable int pageNum, @PathVariable int pageSize) {
        // 调用UserService实现分页查询User
        Page<User> pageInfo = userService.page(new Page<>(pageNum, pageSize));
        return Result.ok(pageInfo);
    }

    /**
     * <p>Title : findPage</p>
     * <p>Description : User分页条件搜索实现</p>
     *
     * @param user
     * @param pageNum  : 当前页
     * @param pageSize : 每页显示多少条
     * @return
     */
    @PostMapping(value = "/search/{pageNum}/{pageSize}")
    public Result<Page<User>> findPage(@RequestBody(required = false) User user, @PathVariable int pageNum, @PathVariable int pageSize) {
        // 调用UserService实现分页条件查询User
        Page<User> pageInfo = userService.page(new Page<>(pageNum, pageSize), SearchUtil.initQuery(user));
        return Result.ok(pageInfo);
    }

    /**
     * <p>Title : findList</p>
     * <p>Description : 多条件搜索User数据</p>
     *
     * @param user
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<User>> findList(@RequestBody(required = false) User user) {
        // 调用UserService实现条件查询User
        List<User> list = userService.list(SearchUtil.initQuery(user));
        return Result.ok(list);
    }

    /**
     * <p>Title : findById</p>
     * <p>Description : 根据ID查询User数据</p>
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    public Result<User> findById(@PathVariable Long id) {
        // 调用UserService实现根据主键查询User
        User user = userService.getById(id);
        return Result.ok(user);
    }

    /**
     * <p>Title : findOne</p>
     * <p>Description : 多条件搜索User数据，结果返回一条</p>
     *
     * @param user
     * @return
     */
    @PostMapping(value = "/findOne")
    public Result<User> findOne(@RequestBody(required = false) User user) {
        // 调用UserService实现条件查询User
        User userResult = userService.getOne(SearchUtil.initQuery(user));
        return Result.ok(userResult);
    }

    /**
     * <p>Title : findAll</p>
     * <p>Description : 查询User全部数据</p>
     *
     * @return
     */
    @GetMapping(value = "/all")
    public Result<List<User>> findAll() {
        // 调用UserService实现查询所有User
        List<User> list = userService.list();
        return Result.ok(list);
    }

    /**
     * 从id位置查询
     * @param id
     * @return
     */
    @GetMapping(value = "/findBaseId/{id}")
    public Result<List<User>> findBaseId(@PathVariable Long id) {
        // 调用UserService实现查询所有User
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("id", id);
        List<User> list = userService.list(queryWrapper);
        return Result.ok(list);
    }

    /**
     * 是否关注
     * @param param
     * @return
     */
    @PostMapping(value = "/followed")
    public Result<Boolean> followed(@RequestBody Map<String, Object> param) {
        String token = param.get("token").toString();
        long followId = Long.parseLong(param.get("followId").toString());
        return Result.ok(userCache.followed(token, followId));
    }

    /**
     * 关注
     * @param param
     * @return
     */
    @PostMapping(value = "/follow")
    public Result<Boolean> follow(@RequestBody Map<String, Object> param) {
        return Result.ok(userService.follow(param));
    }

    /**
     * 校验是否是自己
     * @param param
     * @return
     */
    @PostMapping(value = "/self")
    public Result<Boolean> self(@RequestBody Map<String, Object> param) {
        Long userId = Long.parseLong(param.get("userId").toString());
        String token = param.get("token").toString();
        return Result.ok(userId.equals(userCache.getUserId(token)));
    }

    /**
     * 删除历史记录
     * @param param
     * @return
     */
    @PostMapping(value = "/delViewHistory")
    public Result<Boolean> delViewHistory(@RequestBody Map<String, Object> param) {
        String token = param.get("token").toString();
        long contentId = Long.parseLong(param.get("contentId").toString());
        userCache.delViewHistory(userCache.getUserId(token), contentId);
        return Result.ok(true);
    }

    /**
     * 取消收藏
     * @param param
     * @return
     */
    @PostMapping(value = "/cancelMyCollect")
    public Result<Boolean> cancelMyCollect(@RequestBody Map<String, Object> param) {
        String token = param.get("token").toString();
        long contentId = Long.parseLong(param.get("contentId").toString());
        contentCache.cancelMyCollect(userCache.getUserId(token), contentId);
        return Result.ok(true);
    }

    /**
     * 校验是否重复昵称
     * @param nickName
     * @return
     */
    @PostMapping("/check")
    public Result<Boolean> checkRepeatName(@RequestBody Map<String, Object> param) {
        String nickName = param.get("nickName").toString();
        if (StringUtils.isEmpty(nickName.trim())) {
            return Result.ok(false);
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("nick_name", nickName);
        User one = userService.getOne(queryWrapper);
        return Result.ok(ObjectUtils.isEmpty(one));
    }

    /**
     * 用户信息修改
     * @param user
     * @return
     */
    @PostMapping("/modify")
    public Result<JSONObject> modify(@RequestBody User user) {
        JSONObject modify = userService.modify(user);
        return Result.ok(modify);
    }

    @PostMapping("/userFile")
    public Result<JSONObject> modifyWithFile(@RequestParam(value = "file") MultipartFile file,
                                             @RequestParam(value = "type") String type,
                                             @RequestParam(value = "user") String user) {
        JSONObject jsonObject = userService.modifyWithFile(file, user, type);
        return Result.ok(jsonObject);
    }
}
